package edu.rpi.bt.client;

import com.apress.progwt.client.college.ServiceCache;
import com.apress.progwt.client.domain.User;
import com.apress.progwt.client.domain.commands.SiteCommand;
import com.apress.progwt.client.rpc.EZCallback;
import com.apress.progwt.client.rpc.StdAsyncCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.Position;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.Field;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.TextArea;
import com.gwtext.client.widgets.form.TextField;

import edu.rpi.bt.client.domain.Comment;
import edu.rpi.bt.client.domain.Destination;
import edu.rpi.bt.client.domain.commands.SaveCommentCommand;

public class SaveCommentWindow extends Window{
    private Comment comment;
    private Destination location;
    private long locationId;
    private ServiceCache serviceCache;
    private User thisUser;
    private SaveCommentWindow thisWindow = this;
    private LocationPanelFull locationPanel;
    
    private long destinationId;
    SaveCommentWindow(ServiceCache servCache, Destination loc, User u){
        this.location = loc;
        this.thisUser = u;
        this.setTitle("Add Your Comment");  
        this.serviceCache = servCache;
        this.setClosable(true);
        this.setDraggable(true);
        this.setWidth(570);  
        this.setHeight(380);  
        this.setPlain(true);  
        this.setModal(true);
//this.location=location; //pass location info to comment window
 // init(servCache, location.getId() ); //Pass destination object, and get its id
        this.destinationId=2;
        init(servCache, this.destinationId);
        
        FormPanel formPanel = buildFormPanel();
        
        this.add(new Panel("", "Add your own comments and thoughts on this location.") );
        this.add( formPanel );       
        
    }
    
      
    
    private FormPanel buildFormPanel(){
        final FormPanel formPanel = new FormPanel();  
        final Comment comment = new Comment();
        
        formPanel.setFrame(true);  
        formPanel.setLabelWidth(90);
        formPanel.setLabelAlign(Position.TOP);
        
        TextField nameField = new TextField("Name", "name");
        nameField.setWidth(425);
        nameField.setValue( thisUser.getNickname() );
        nameField.setReadOnly(true);
        formPanel.add( nameField );  

        TextArea commentArea = new TextArea("Your Comment", "comment");
        commentArea.setWidth(425);
        commentArea.setHeight(200);
        commentArea.setAllowBlank(false);  
        formPanel.add(commentArea);  

        //icon button  
        Button saveButton = new Button("Save", new ButtonListenerAdapter() {  
            public void onClick(Button button, EventObject e) {
                // MessageBox.alert("user is " + thisUser.getNickname());
                String commentText=  null;
                Field[] fields = formPanel.getFields();
                for( int i=0; i < fields.length; i++ ){
                    if( fields[i].getName() == "comment"){
                        commentText =  fields[i].getValueAsString();
                    }
                }
                
                if( commentText == null ){
                    MessageBox.alert("Error: You must enter a comment!");
                    return;
                }

                comment.setComment(commentText + " - <span class=\"user\">" + thisUser.getNickname() + "</span>");
                comment.setDestination(location);
                comment.setUser(thisUser);
                
                
                SaveCommentCommand comm = new SaveCommentCommand(
                        location, comment);
                //MessageBox.alert("command");
                serviceCache.executeCommand(comm,
                        new EZCallback<SiteCommand>() {
                    // MessageBox.alert("execute");
                    public void onSuccess( SiteCommand success) {

                        SaveCommentCommand rtn = (SaveCommentCommand) success;

                        comment.setId(rtn.getSavedCommentID());
                        
                        //MessageBox.alert("Comment id " + comment.getId());
                        
                        locationPanel.reinit();
                        
                        thisWindow.close();
                    }
                });
            }  
        });  

        formPanel.add(saveButton);  

        return formPanel;
    }
    
    private void init(ServiceCache servCache, long locationId){
        //    this.serviceCache = servCache;
            
    
            serviceCache
            .getCurrentUser(new StdAsyncCallback<User>("Get User") {
    
                public void onSuccess(User result) {
                    super.onSuccess(result);
                    if (result == null) {
                     
                    } else {
                       
                        load(result);
                    }
                }
    
                public void onFailure(Throwable caught) {
                    super.onFailure(caught);
                }
            });
    }
    
         
        
        public void load(Destination dest){
        //    MessageBox.alert("adding destination"); 
            this.location=dest;
       //     MessageBox.alert("Location Name " + location.getName() ); 
                
             
        }
        
        public  Destination getDestinations(){
         //   System.out.println("inside getdests");
       //     MessageBox.alert("inside get destination..count "); 
            return this.location;
        }
        
    
    
    public void load(User user){
        System.out.println("Bannana..load User " + user.getNickname());
        this.thisUser=user;
    }   

    public void setLocationPanel( LocationPanelFull lpf ){
        this.locationPanel = lpf;
    }
    
}
